package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;


@Service
@Slf4j
@Transactional
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Override
    public void saveWithDishFlavor(DishDto dishDto) {
        //处理基本数据
        this.save(dishDto);

        //基本数据存储到dish表后会自动生成菜品id,获取它
        Long dishid = dishDto.getId();

        List<DishFlavor> flavors = dishDto.getFlavors();
        //遍历flavors集合,为每一个dishFlavor对象设置id并添加
        if(flavors != null && flavors.size() > 0 ){
            for (DishFlavor flavor : flavors) {
                flavor.setDishId(dishid);
            }
            dishFlavorService.saveBatch(flavors);
        }
    }



}
